package cn.sym.user.service.impl;

import cn.sym.basic.baseConstant.BaseConstant;
import cn.sym.basic.dto.RegisterDto;
import cn.sym.basic.exception.MyException;
import cn.sym.basic.service.impl.BaseServiceImpl;
import cn.sym.basic.util.MD5Utils;
import cn.sym.basic.util.StrUtils;
import cn.sym.user.domain.Logininfo;
import cn.sym.user.domain.User;
import cn.sym.user.mapper.LogininfoMapper;
import cn.sym.user.mapper.UserMapper;
import cn.sym.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sym
 * @since 2022-02-14
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void phoneRegister(RegisterDto registerDto) {
        //1.空值校验 短信验证码，密码，确认密码
        if (StringUtils.isEmpty(registerDto.getImageCode())||StringUtils.isEmpty(registerDto.getPassword())||StringUtils.isEmpty(registerDto.getConfirmPassword())){
            throw new MyException("填写的信息不能为空！！！");
        }
        //2.判断短信验证码是否有效
        String smsCodeKey = BaseConstant.SmsConstant.BUSINESS_REGISTER_PREFIX+registerDto.getPhone();
        Object smsCodeTmp = redisTemplate.opsForValue().get(smsCodeKey);
        //2.1如果通过key获取到的值为空，说明短信验证码已经过期
        if (smsCodeTmp==null){
            throw new MyException("验证码已经失效，请重新获取验证码！！");
        }
        String codeTmp = smsCodeTmp.toString().split(":")[0]; //使用历史的验证码
        if (!registerDto.getSmsCode().equalsIgnoreCase(codeTmp)){
            throw new MyException("请填写正确的验证码！！");
        }
        //3.判断两次输入的密码是否一致
        if (!registerDto.getPassword().equals(registerDto.getConfirmPassword())){
            throw new MyException("两次输入的验证码不一致！");
        }
        //4.存入数据库t_user  同时级联保存到t_logininfo
        //4.1调用dto2User方法初始化user
        User user = dto2User(registerDto);
        Logininfo logininfo = user2Logininfo(user);
        logininfoMapper.add(logininfo);//返回自增ID
        user.setLogininfoId(logininfo.getId());
        userMapper.add(user);
    }

    private Logininfo user2Logininfo(User user) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        //表示登录人是用户而不是管理员
        logininfo.setType(1);
        logininfo.setDisable(1);
        return logininfo;
    }

    private User dto2User(RegisterDto registerDto) {
        User user = new User();
        user.setUsername(registerDto.getPhone());
        user.setPhone(registerDto.getPhone());
        //盐值
        String salt = StrUtils.getComplexRandomString(32);
        //使用Md5 加密 加盐
        String md5Pwd = MD5Utils.encrypByMd5(registerDto.getPassword() + salt);
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        user.setState(1);
        return user;
    }
}
